﻿//===============================================================================
//	MapSlot.cpp
//	地图相关处理
//	地图及其内部相关所有处理
//===============================================================================

#include "MapSlot.h"
#include "GameTimer.h"
#include "../share/user_define.h"
#include "Player.h"
#include "ManagerHelper.h"
#include "BaseMessage.h"
#include "LuaScriptManager.h"
#include "ConstantDefine.h"

#include "script_define.h"
#include "game_msg.h"
#include "GameServer.h"
#include "game_logic.h"
#include "scene_logic.h"
#include "playerinfo_mgr.h"
#include "../share/GameHostManager.h"
#include "../share/time_module.h"

//-------------------------------------------------------------------------------
void MapSlot::allMapUpdate(void)
{
	_safe_guard_begin;

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void MapSlot::allMapHeartbeat(void)
{
	_safe_guard_begin;


	_safe_guard_end;
}

//-------------------------------------------------------------------------------
MapSlot::MapSlot()
	: m_timer(new GameTimer(1000))
	, m_run_timer(new GameTimer(0))
	, m_second_timer(new GameTimer(997))
	, m_slot_lua_ptr(nullptr)
	, m_current_count(0)
	, m_slot_times(0)
	, m_slot_count(0)
	, m_slot_all(0)
{
	_safe_guard_begin;

	_safe_check(g_lua_script_manager->UpdateToLuaManager(m_slot_lua_ptr));

	m_slot_lua_ptr->RunScriptFunction(LUA_PlayerManagerScript, LUA_LoadArenaRankingsToRedis);

	//for (auto &_it : m_map_vec)
	//{
	//	//_it.SetupSceneComponents();
	//	_it.SetupSocketMessage();
	//	_it.SetupTimer(334/*251,307*/, 997);
	//	_it.SetupFinished();
	//}

	last_update_time_ = 0;
	m_second_timer->Startup();

	_safe_guard_end;
}

//-------------------------------------------------------------------------------
MapSlot::~MapSlot()
{
	_safe_delete(m_second_timer);
	_safe_delete(m_timer);
	_safe_delete(m_run_timer);

}

//-------------------------------------------------------------------------------
void MapSlot::AddScene(uint32_t sceneId, uint32_t scriptId)
{
	_safe_guard_begin;

	//if (m_current_count >= default_map_count)
	//{
	//	_safe_check_ex(false, "Map manager @ is to max count!");
	//}

	////	指定相关场景id
	//m_map_vec[m_current_count].SetSceneId(sceneId);
	//m_map_vec[m_current_count].SetScriptId(scriptId);
	//m_map_vec[m_current_count].SetGrid(MapGridMgrPtr->GetMapGrid(sceneId));

	////	场景状态为运行中
	//m_map_vec[m_current_count].SetSceneState(scene_state::running);

	////	初始化场景
	//m_map_vec[m_current_count].InitializeScene();

	//++m_current_count;

	_safe_guard_end;
}


//-------------------------------------------------------------------------------
void MapSlot::SetMapHasData(uint32_t sceneId)
{
	_safe_guard_begin;



	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void MapSlot::RemoveMapElement(uint32_t sceneId, uint32_t key)
{
	_safe_guard_begin;


	_safe_guard_end;
}

//-------------------------------------------------------------------------------
void MapSlot::InitializeSecne(void)
{
	_safe_guard_begin;


	_safe_guard_end;
}



//-------------------------------------------------------------------------------
bool MapSlot::checkIsRightGMMsg(uint32_t protocol_number)
{
	_safe_guard_begin;

	static const uint32_t s_process_number[] = {
		common_redpoint_check_notify,
		common_player_hero_list_req,
		pet_query_list_req,
		common_use_lucky_draw_req,
		common_player_friend_list_req,
		common_ask_offer_reward_mission_req,
		common_backpack_list_req,
		common_mission_complete_list_req,
		common_rechange_finished_req,

		login_get_gamesrv_info_req,
		gm_public_message_notify,
		gm_public_mail_notify,
		gm_test_message_notify,
		server_slot_check_notify,
		db_reload_player_info_to_redis_ret,
		db_register_player_info_to_redis_ret,
		db_select_some_info_ret,
		db_select_single_info_ret,
		db_select_info_amount_ret,
		db_player_add_friend_ret,
		db_player_allow_add_friend_ret,
		db_player_create_union_ret,
		db_player_disband_union_ret,
		db_player_approve_someone_ret,
		db_union_kickout_someone_ret,
		db_union_change_someone_position_ret,
		db_union_query_detailed_info_ret,
		db_query_rank_list_ret,
		db_rank_player_info_ret,
		db_player_query_arena_list_ret,
		db_player_arena_settlement_ret,
		db_search_moba_list_ret,
		db_player_mail_list_ret,
		db_init_arena_fight_ret,
		db_player_donation_ret,
		db_union_uplevel_info_ret,
		db_union_list_ret,
		db_search_someone_union_ret,
		db_union_all_member_ret,
		db_player_friend_list_ret,
		db_init_friend_fight_ret,
		db_friend_quick_action_ret,
		db_query_player_info_ret,
		db_exchange_cdkey_item_ret,
		db_delete_friend_ret,
		db_world_boss_rank_list_ret,
		db_world_boss_rank_info_ret,
		db_draw_first_rechange_box_ret,
		db_worldboss_list_ret,
		db_init_player_expediton_list_ret,
		db_update_player_expediton_gate_ret,
		db_update_player_expediton_info_ret,

		db_player_rechanged_notify,

		lg_check_account_ret,
		lg_get_gamesrv_info_ret,
		lg_create_player_ret,
		lg_player_login_ret

	};

	for (int32_t i = 0; i < sizeof(s_process_number) / sizeof(uint32_t); ++i)
	{
		if (protocol_number == s_process_number[i])
		{
			return true;
		}
	}

	return  false;

	_safe_guard_end;

	return false;
}

void MapSlot::Init()
{
	InitializeSecne();

// 	std::vector<SceneConfigureInfo> _all_scence;
// 	SceneConfigureTablePtr->GetAllSceneConfigureInfoById(_all_scence);
// 
// 	for (auto &_it : _all_scence)
// 	{
// 		if (_it.m_thread_id == 0)
// 		{
// 			printf("[%d] thread id is 0, Not Dispatch to slot !!!\n", _it.m_key);
// 			continue;
// 		}
// 		loadSceneById(_it.m_key, _it.m_script_id);
// 	}
}

void MapSlot::ChangeGameSrvOnlineAmountToRedisDB()
{
	_safe_guard_begin;

	_safe_check(m_slot_lua_ptr != nullptr);

	int32_t _count = 0;
	int32_t _hostId = (int32_t)GameHostManager::GetInstance().GetHostId();
	int32_t _port = (int32_t)GameHostManager::GetInstance().GetGameHostListenPort();

	std::string _ip = GameHostManager::GetInstance().GetGameHostIp();

	if (m_slot_lua_ptr->TryLock())
	{
		LOG_INFO("default",">> onlinePlayer msg count: %d", _count);

		m_slot_lua_ptr->RunScriptFunction(LUA_PlayerManagerScript
			, LUA_ChangeGameSrvOnlineAmount, _hostId, _port, _count, _ip.c_str());
	}
	else
	{
		m_slot_lua_ptr->RunScriptFunction(LUA_PlayerManagerScript
			, LUA_ChangeGameSrvOnlineAmount, _hostId, _port, _count, _ip.c_str());
	}
	_safe_guard_end;
}

void MapSlot::AddAffixedNode(uint32_t key, uint32_t missionId, uint32_t scriptId)
{
	_safe_guard_begin;

	DailyMissionAffixNode dt;
	dt.SetAffixedNode(missionId, scriptId);

	m_DailyAffixNode.insert(std::make_pair(key, dt));

	_safe_guard_end;
}

DailyMissionAffixNode* MapSlot::GetAffiexNodeDataByKey(uint32_t unlockId)
{
	_safe_guard_begin;

	auto it = m_DailyAffixNode.find(unlockId);
	if (it == m_DailyAffixNode.end())
	{
		return nullptr;
	}
	else
	{
		return &(it->second);
	}

	_safe_guard_end;

	return nullptr;
}

void MapSlot::TickUpdate()
{
	m_run_timer->ResetStartTime();
	uint32_t ddt = 0;
	uint32_t _dt = m_run_timer->GetElapsed();

	CSceneLogic::GetInstance().PlayerDestroyDoing();

	//	地图数据更新
// 	allMapUpdate();
// 
// 	//	地图heartbeat
// 	allMapHeartbeat();


	if (_dt > 1024)
	{
		LOG_WARN("default","WARNING >> MapSlot run one loop is %d millisecond.", _dt);
	}

	//	日志输出
	++m_slot_count;
	m_slot_all += _dt;
	m_slot_times += _dt;
	if (m_slot_times > 30000)
	{
		LOG_WARN("default","MapSlot every 30 second: %d / %d, average time: %d, running time: %lld", m_slot_times, m_slot_count, (m_slot_times / m_slot_count), (m_slot_all >> 10));
		m_slot_times = 0;
		m_slot_count = 0;
	}

	uint64_t now_time_ms = CUR_MS;
	if (last_update_time_ + 500 < now_time_ms || last_update_time_ == 0)
	{
		CPlayerInfoMgr::GetInstance().SaveRoles(10);
		last_update_time_ = now_time_ms;
	}

	uint64_t now = CUR_MS;
	if (ServerHeartMgrPtr)
	{
		ServerHeartMgrPtr->Heartbeat(now);
	}
}
